import json

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.views import View


class register_view(View):
    """
    1. 类视图 继承自View
    2. 根据不同的请求方式，写与其同名的函数
    """

    def get(self, request):
        return HttpResponse('注册页面')

    def post(self, request):
        return HttpResponse('注册逻辑的实现')


class login_view(View):
    """
    使用get和post在url中传参。
    """
    def get(self, request):
        GET_dict = request.GET
        print('GET_dict', GET_dict)
        # name有多个时，使用getlist获取多个值
        name = request.GET.getlist("name")
        age = request.GET.get("age")
        return HttpResponse('{}----{}'.format(name, age))

    def post(self, request):
        GET_dict = request.GET
        print('GET_dict', GET_dict)
        # name有多个时，使用getlist获取多个值
        name = request.GET.getlist("name")
        age = request.GET.get("age")
        return HttpResponse('登录逻辑{}----{}'.format(name, age))


class formdataview(View):

    def post(self, request):
        name = request.POST.get("name")
        age = request.POST.get("age")
        return HttpResponse('{}----{}'.format(name, age))


class jsondataview(View):

    def post(self, request):
        json_body = request.body
        json_dict = json.loads(json_body)

        name = json_dict.get("name")
        age = json_dict.get("age")
        gender = json_dict.get("gender")

        return HttpResponse('{}----{}----{}'.format(name, age, gender))


class urldataview(View):

    def get(self, request, age1):
        return HttpResponse('{}'.format(age1))


class mobiledataview(View):

    def get(self, request, phone_num):
        return HttpResponse('{}'.format(phone_num))

class request_head_view(View):

    def get(self, request):
        type = request.META.get('CONTENT_TYPE')
        print(type)
        cookie = request.COOKIES
        print(cookie)
        files = request.FILES
        print(files)
        path = request.path
        print(path)
        method = request.method
        print(method)
        user = request.user
        print(user)
        return HttpResponse('{}'.format(cookie))
